package j03return;

public class J1 {
    public static void main(String[] args){
//        int result = test1();
//        System.out.println(result);//10

        System.out.println(test2());//10
    }
    public static int test2(){
        int i = 1;
        try{
            i++;//2
            System.out.println(10/0);
        }catch(Exception e){
            i--;//1
            System.out.println("catch block i = "+i);//1
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);//10
        }
        return i;
    }

    public static int test1(){
        int i = 1;
        try{
            i++;
            System.out.println("try block, i = "+i);//2
        }catch(Exception e){
            i--;
            System.out.println("catch block i = "+i);
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);//10
        }
        return i;
    }


}

class J2{
    public static void main(String[] args){
        int result = test3();
        //2   2
        System.out.println(result);
    }
    public static int test3(){
        int i = 1;
        try{
            i++;//2
            System.exit(0);//强制退出
            System.out.println("try block, i = "+i);//2
            /*
             try遇到return
                1，程序不会终止，继续前进
                2，保留当前的return 结果（镜像）
                3，如果finally也有return，会覆盖前面的return
             */
            return i;
        }catch(Exception e){
            i ++;
            System.out.println("catch block i = "+i);
            return i;
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);//10

            return 999;
        }
    }

}
